package bean;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.render.ClientEvent;

import oracle.ui.pattern.dynamicShell.TabContext;

public class Launcher {
    private boolean editMode;
    
    private String mode;

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getMode() {
        return mode;
    }

    public Launcher() {
        launchActivity("Departments", "/WEB-INF/departments-tf.xml#departments-tf", false, null);
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public void createDepartment() {
        this.mode = "CREATION";
    }
    
    public void hideForm() {
        this.editMode = false;
    }
    
    public void updateDepartment() {
        this.mode = "EDITION";
    }

    public void launchDepartmentActivity(ClientEvent clientEvent) {
        launchActivity("Departments", "/WEB-INF/departments-tf.xml#departments-tf", false, null);
    }
    
    public void launchEmployeeActivity(ClientEvent clientEvent) {
        launchActivity("Employees", "/WEB-INF/employees-tf.xml#employees-tf", false, null);
    }
    
    public void launchEmployeeDetailActivity(ActionEvent actionEvent) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        // TODO : check param null
        parameters.put("pEmployeeId", actionEvent.getComponent().getAttributes().get("pEmployeeId"));
        launchActivity("Employee Detail", "/WEB-INF/employee-detail-tf.xml#employee-detail-tf", true, parameters);
    }
    
    public void closeCurrentTab() {
        TabContext.getCurrentInstance().removeCurrentTab();
    }


    private void launchActivity(String title, String taskflowId, boolean newTab, Map parameters) {
        try {
            if (newTab) {
                TabContext tabContext = TabContext.getCurrentInstance();
                tabContext.addTab(title, taskflowId, parameters);
            } else {
                TabContext.getCurrentInstance().addOrSelectTab(title, taskflowId, parameters);
            }
        } catch (TabContext.TabOverflowException toe) {
            // causes a dialog to be displayed to the user saying that there are
            // too many tabs open - the new tab will not be opened...
            toe.handleDefault();
        }
    }
}
